/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wypozyczalnia;

import commons.AbstractDAO;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Adrian
 */
public class UjemneSaldoDAO extends AbstractDAO {
    private Double saldo;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date data = new Date();
    
    public UjemneSaldoDAO() {
        super();
    }

    public String zerujSaldo(String id) {
        try {
            query = "UPDATE `biblioteka`.`uzytkownicy` SET `saldo` = '0.00' WHERE ident_biblioteczny =?;";
            prepStmt = conn.prepareStatement(query);
            prepStmt.setString(1,id);
            prepStmt.executeUpdate();
            return "ok";
        } catch (SQLException ex) {
            Logger.getLogger(UjemneSaldoDAO.class.getName()).log(Level.SEVERE, null, ex);
            return "blad";
        }
    }

    public String wplac(String id,Double kwota) {
        try {
            query = "SELECT saldo FROM uzytkownicy WHERE ident_biblioteczny = ?";
            prepStmt = conn.prepareStatement(query);
            prepStmt.setString(1,id);
            rs = prepStmt.executeQuery();
            while(rs.next()){
                saldo=rs.getDouble("saldo");
            }
            kwota=Math.abs(kwota);
            saldo+=kwota;
            query = "UPDATE `biblioteka`.`uzytkownicy` SET `saldo` = ? WHERE ident_biblioteczny =?;";
            prepStmt = conn.prepareStatement(query);
            prepStmt.setDouble(1,saldo);
            prepStmt.setString(2,id);
            prepStmt.executeUpdate();
            query = "INSERT INTO `biblioteka`.`wplaty` (`data_wplaty`, `kwota`, `ident_biblioteczny`) VALUES (?,?,?);";
            prepStmt = conn.prepareStatement(query);
            prepStmt.setString(1,sdf.format(data));
            prepStmt.setDouble(2,kwota);
            prepStmt.setString(3,id);
            prepStmt.executeUpdate();
            return "ok";
        } catch (SQLException ex) {
            error("Wystąpił błąd. Sprawdź czy wprowadzona kwota jest poprawna.");
            Logger.getLogger(UjemneSaldoDAO.class.getName()).log(Level.SEVERE, null, ex);
            return "blad";
        }
    }
    public ArrayList<SaldoData> getListaSald() {
        ArrayList<SaldoData> list = new ArrayList<SaldoData>();
        try {
            query = "SELECT ident_biblioteczny, imie,nazwisko,saldo FROM uzytkownicy WHERE saldo < 0";
            prepStmt = conn.prepareStatement(query);
            rs = prepStmt.executeQuery();

            while (rs.next()) {
                SaldoData data = new SaldoData();
                data.setId(rs.getString(1));
                data.setImie(rs.getString(2));
                data.setNazwisko(rs.getString(3));
                data.setSaldo(rs.getDouble(4));
                list.add(data);
            }
            return list;

        } catch (SQLException ex) {
            Logger.getLogger(UjemneSaldoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }
}
